<template>
  <el-dialog
    :class="'el-dialog-theme3'"
    v-model="isShowDialog"
    fullscreen
    class="ptDialog roll-dialog pub_dialog"
    width="90%"
    title="选择检测项目"
    top="1vh"
    draggable
    :close-on-click-modal="false"
    align-center
    @close="closeDialog"
  >
    <el-tabs v-model="jcxmTabName">
      <!-- 检测项目 -->
      <el-tab-pane label="检测项目" name="first">
        <selectJcxm
          ref="jcxmRef"
          :params="params"
          @handleJcxm="handleJcxm"
          @getMJIds="getMJIds"
          @handleQuerySelectedJcxmList="handleQuerySelectedJcxmList"
          v-if="jcxmTabName == 'first'"
        />
      </el-tab-pane>
      <!-- /检测项目 -->

      <!-- 检测项目组 -->
      <el-tab-pane label="检测项目组" name="two">
        <selectJcxmGroup
          ref="jcxmGroupRef"
          :params="params"
          @handleJcxm="handleJcxm"
          @handleUpdateIsSum="handleUpdateIsSum"
          @getMJIds="getMJIds"
          @handleQuerySelectedJcxmList="handleQuerySelectedJcxmList"
          v-if="jcxmTabName == 'two'"
        />
      </el-tab-pane>
      <!-- /检测项目组 -->
    </el-tabs>
    <el-row>
      <el-col :span="24" style="text-align: right; margin-top: 20px; padding-right: 20px">
        <span style="margin-right: 15px">已选择 {{ jcxmTotal }} 条</span>
        <el-button type="danger" @click="handleClearSelected">全部删除</el-button>
        <el-button
          type="success"
          :disabled="selecedMJIds.length == 0"
          @click="handleSaveJcxm"
          >确定</el-button
        >
        <el-button type="success" @click="closeDialog">关闭</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script setup>
import { addJcxm, seletedJcxmList, clearSelected } from "@/api/store/package";
const { proxy } = getCurrentInstance();
const isShowDialog = ref(false); // 控制dialog开启/关闭
import selectJcxm from "./selectJcxm";
import selectJcxmGroup from "./selectJcxmGroup";
const jcxmTabName = ref("first"); // 选项卡默认选中
const params = ref();
const jcxmTotal = ref(0);
const selecedMJIds = ref([]);
const selectdShow = ref(false);
const closeStatus = ref(2); //2:关闭不刷新父页面中的已选检测项目列表。1：刷新
// 对外暴露以下方法或变量
defineExpose({
  isShowDialog,
  setParams,
});
function setParams(data) {
  params.value = data;
}

function handleJcxm(data) {
  const row = data.row;
  const index = data.index;
  if (index == -1) {
    let index = 0;
    for (const selectRow of selecedMJIds.value) {
      if (selectRow.mJId == row.mJId) {
        if (row.children && row.children.length == 1) {
          //从检测项目组是删除。
          //删除组内的单个检测项目
          let index1 = 0;
          for (const singleJcxm of selectRow.children) {
            if (singleJcxm.mJId == row.children[0].mJId) {
              selectRow.children.splice(index1, 1);
              break;
            }
            index1++;
          }
        } else {
          //单个检测项目删除。
          selecedMJIds.value.splice(index, 1);
        }
        break;
      }
      index++;
    }
    getJcxmTotalCount();
    return;
  }
  if (row.children) {
    let isMatch = false;
    for (const selectRow of selecedMJIds.value) {
      if (selectRow.mJId == row.mJId) {
        selectRow.children = selectRow.children.concat(row.children);
        isMatch = true;
        break;
      }
    }
    if (!isMatch) {
      selecedMJIds.value.push(row);
    }
    getJcxmTotalCount();
    return;
  }
  selecedMJIds.value.push(row);
  getJcxmTotalCount();
}

function handleUpdateIsSum(jcxmGroup) {
  for (const selectRow of selecedMJIds.value) {
    if (jcxmGroup.mJId == selectRow.mJId && selectRow.children) {
      selectRow.isSum = jcxmGroup.isSum;
      selectRow.children.forEach((item) => {
        item.isSum = jcxmGroup.isSum;
      });
      break;
    }
  }
}

function handleSaveJcxm() {
  let data = { packageId: params.value.packageId };
  const packageJcxmList = [];
  for (const selectJcxm of selecedMJIds.value) {
    if (selectJcxm.children) {
      const mJIds = selectJcxm.children.map((item) => {
        return item.mJId;
      });
      packageJcxmList.push({
        mJId: selectJcxm.mJId,
        isSum: selectJcxm.isSum,
        mJIds: mJIds,
      });
      continue;
    }
    packageJcxmList.push({ mJId: selectJcxm.mJId });
  }
  data.packageJcxmList = packageJcxmList;
  addJcxm(data).then((res) => {
    proxy.$modal.msgSuccess("保存成功");
    isShowDialog.value = false;
    closeStatus.value = 1;
    closeDialog();
  });
}

function handleQuerySelectedJcxmList(callback) {
  if (selecedMJIds.value.length > 0) {
    getMJIds(callback);
    return;
  }
  seletedJcxmList(params.value.packageId).then((res) => {
    selecedMJIds.value = res.data;
    getMJIds(callback);
    getJcxmTotalCount();
  });
}

function handleClearSelected() {
  proxy.$modal.confirm("确定要全部删除吗？").then(function () {
    clearSelected(params.value.packageId).then((res) => {
      proxy.$modal.msgSuccess("删除成功");
      nextTick(() => {
        if (jcxmTabName.value == "first") {
          proxy.$refs["jcxmRef"].getList();
        } else {
          proxy.$refs["jcxmGroupRef"].getList();
        }
      });
      selecedMJIds.value = [];
      getJcxmTotalCount();
      closeStatus.value = 1;
    });
  });
}

function getMJIds(callback) {
  let mJIds = [];
  for (const item of selecedMJIds.value) {
    const item1 = { mJId: item.mJId, isSum: item.isSum };
    if (item.children) {
      item1.children = item.children.map((item1) => {
        return { mJId: item1.mJId, groupId: item1.groupId };
      });
    }
    mJIds.push(item1);
  }
  callback(mJIds);
}

function closeDialog() {
  emits("dialogClose", closeStatus.value);
}

function getJcxmTotalCount() {
  let count = 0;
  selecedMJIds.value.forEach((item) => {
    if (item.children) {
      count += item.children.length;
    } else {
      count++;
    }
  });
  jcxmTotal.value = count;
}

const emits = defineEmits(["dialogClose"]);
</script>
